import uvicorn
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from code.app.config import HttpMiddleware, LoggerConfig
from code.app.config.exception_handler import global_exception_handler
from code.app.controllers.file import file_app
from code.app.controllers.image_generation import image_app
from code.app.controllers.out_painting import out_painting_app
from code.app.controllers.portrait_style_repaint import portrait_app

# 确保日志配置在程序的开始部分
logger_config = LoggerConfig()

# 初始化应用
app = FastAPI()


# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有请求来源
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有 HTTP 方法
    allow_headers=["*"],  # 允许所有请求头
)
# 配置RequestID中间件
app.add_middleware(HttpMiddleware)

# 注册全局异常处理器
app.add_exception_handler(Exception, global_exception_handler)

# 配置接口路由
app.include_router(image_app, tags=["图像生成-通用接口"])
app.include_router(portrait_app, tags=["人像风格重绘"])
app.include_router(out_painting_app, tags=["图像画面扩展"])
app.include_router(file_app, tags=["文件管理"])


if __name__ == "__main__":
    # 本地运行
    uvicorn.run(app, host="127.0.0.1", port=8000)
    # 函数计算部署
    # uvicorn.run(app, host="0.0.0.0", port=80)
